package systemInformation;

import java.util.Vector;

import org.snmp4j.PDU;
import org.snmp4j.agent.mo.snmp.DisplayString;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.TimeTicks;
import org.snmp4j.smi.VariableBinding;

import features.SNMPException;
import features.SNMPSession;
import features.SNMPValue;

public class BasicInfo {
	private final static String SYSTEM_DESCRIPTION_OID =".1.3.6.1.2.1.1.1.0";
	
	private final static String UP_TIME_OID = 			".1.3.6.1.2.1.1.3.0";
	
	private final static String SYSTEM_CONTACT_OID = 	".1.3.6.1.2.1.1.4.0";
	
	private final static String SYSTEM_NAME_OID = 		".1.3.6.1.2.1.1.5.0";

	private final static String SYSTEM_LOCATION_OID = 	".1.3.6.1.2.1.1.6.0";

	private final static String MEMORY_SIZE_OID = 		".1.3.6.1.2.1.25.2.2.0";

	// private final static OID CPU_COUNT_OID = 	new OID("");
	//
	// private final static OID CPU_TYPE_OID = 		new OID("");
	//

	private String systemName;

	private String upTime;

	private String systemLocation;

	private String systemContact;

	private String systemDescription;

	private int cpuCount;

	private String cpuType;

	private int memorySize; // in MB

	@SuppressWarnings("unchecked")
	public static BasicInfo fromPDU(PDU pdu) {
		BasicInfo info = new BasicInfo();
		Vector<VariableBinding> variableBindings = pdu.getVariableBindings();

		for (VariableBinding variableBinding : variableBindings) {
			// if(variableBinding.getOid().compareTo(CPU_COUNT_OID) == 0) {
			// info.setCpuCount(variableBinding.getVariable().toInt());
			// } else if(variableBinding.getOid().compareTo(CPU_TYPE_OID) == 0)
			// {
			// info.setCpuType(variableBinding.getVariable().toString());
//			 } else 
			if(variableBinding.getOid().compareTo(new OID(MEMORY_SIZE_OID)) == 0) {
				info.setMemorySize(variableBinding.getVariable().toInt());
			} else if (variableBinding.getOid().compareTo(new OID(SYSTEM_CONTACT_OID)) == 0) {
				info.setSystemContact(((DisplayString) variableBinding.getVariable()).toString());
			} else if (variableBinding.getOid().compareTo(new OID(SYSTEM_DESCRIPTION_OID)) == 0) {
				info.setSystemDescription(((DisplayString) variableBinding.getVariable()).toString());
			} else if (variableBinding.getOid().compareTo(new OID(SYSTEM_LOCATION_OID)) == 0) {
				info.setSystemLocation(((DisplayString) variableBinding.getVariable()).toString());
			} else if (variableBinding.getOid().compareTo(new OID(SYSTEM_NAME_OID)) == 0) {
				info.setSystemName(((DisplayString) variableBinding.getVariable()).toString());
			} else if (variableBinding.getOid().compareTo(new OID(UP_TIME_OID)) == 0) {
				info.setUpTime(((TimeTicks) variableBinding.getVariable()).toString());
			}
		}

		return info;
	}

	public static BasicInfo fromSession(SNMPSession session) throws SNMPException {
		BasicInfo info = new BasicInfo();
		
		SNMPValue v1 = null;
		v1 = session.getSingleValue(SYSTEM_DESCRIPTION_OID);
		info.setSystemDescription(v1.toString());
		v1 = session.getSingleValue(UP_TIME_OID);
		info.setUpTime(v1.toString());
		v1 = session.getSingleValue(SYSTEM_CONTACT_OID);
		info.setSystemContact(v1.toString());
		v1 = session.getSingleValue(SYSTEM_NAME_OID);
		info.setSystemName(v1.toString());
		v1 = session.getSingleValue(SYSTEM_LOCATION_OID);
		info.setSystemLocation(v1.toString());
		v1 = session.getSingleValue(MEMORY_SIZE_OID);
		info.setMemorySize(v1.toInt());
		return info;
	}
	
	public int getCpuCount() {
		return this.cpuCount;
	}

	public void setCpuCount(int cpuCount) {
		this.cpuCount = cpuCount;
	}

	public String getCpuType() {
		return this.cpuType;
	}

	public void setCpuType(String cpuType) {
		this.cpuType = cpuType;
	}

	public int getMemorySize() {
		return this.memorySize;
	}

	public void setMemorySize(int memorySize) {
		this.memorySize = memorySize;
	}

	public String getSystemContact() {
		return this.systemContact;
	}

	public void setSystemContact(String systemContact) {
		this.systemContact = systemContact;
	}

	public String getSystemDescription() {
		return this.systemDescription;
	}

	public void setSystemDescription(String systemDescription) {
		this.systemDescription = systemDescription;
	}

	public String getSystemLocation() {
		return this.systemLocation;
	}

	public void setSystemLocation(String systemLocation) {
		this.systemLocation = systemLocation;
	}

	public String getSystemName() {
		return this.systemName;
	}

	public void setSystemName(String systemName) {
		this.systemName = systemName;
	}

	public String getUpTime() {
		return this.upTime;
	}

	public void setUpTime(String upTime) {
		this.upTime = upTime;
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + this.cpuCount;
		result = PRIME * result
				+ ((this.cpuType == null) ? 0 : this.cpuType.hashCode());
		result = PRIME * result + this.memorySize;
		result = PRIME
				* result
				+ ((this.systemContact == null) ? 0 : this.systemContact
						.hashCode());
		result = PRIME
				* result
				+ ((this.systemDescription == null) ? 0
						: this.systemDescription.hashCode());
		result = PRIME
				* result
				+ ((this.systemLocation == null) ? 0 : this.systemLocation
						.hashCode());
		result = PRIME * result
				+ ((this.systemName == null) ? 0 : this.systemName.hashCode());
		result = PRIME * result
				+ ((this.upTime == null) ? 0 : this.upTime.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final BasicInfo other = (BasicInfo) obj;
		if (this.cpuCount != other.cpuCount)
			return false;
		if (this.cpuType == null) {
			if (other.cpuType != null)
				return false;
		} else if (!this.cpuType.equals(other.cpuType))
			return false;
		if (this.memorySize != other.memorySize)
			return false;
		if (this.systemContact == null) {
			if (other.systemContact != null)
				return false;
		} else if (!this.systemContact.equals(other.systemContact))
			return false;
		if (this.systemDescription == null) {
			if (other.systemDescription != null)
				return false;
		} else if (!this.systemDescription.equals(other.systemDescription))
			return false;
		if (this.systemLocation == null) {
			if (other.systemLocation != null)
				return false;
		} else if (!this.systemLocation.equals(other.systemLocation))
			return false;
		if (this.systemName == null) {
			if (other.systemName != null)
				return false;
		} else if (!this.systemName.equals(other.systemName))
			return false;
		if (this.upTime == null) {
			if (other.upTime != null)
				return false;
		} else if (!this.upTime.equals(other.upTime))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		return "BasicInfo [" + cpuType + ", " + cpuType + ", " + memorySize + ", " + systemContact + ", " + systemDescription + ", " + systemLocation + ", " + systemName + ", " + upTime + "]";
	}
}
